#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class RoleListProvider : CY.GFive.Core.Providers.DALProvider.IRoleListProvider
	{
		#region Sql
		private static readonly string SqlSelectRoleList = "SELECT [Id], [RoleName] FROM [RoleList] WHERE  [Id]=@Id";

		private static readonly string SqlInsertRoleList = "INSERT INTO [RoleList]([RoleName]) values(@RoleName);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateRoleList = "UPDATE [RoleList] SET [RoleName] = @RoleName WHERE  [Id]=@Id";

		private static readonly string SqlDeleteRoleList = "DELETE [RoleList] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllRoleList = "SELECT [Id], [RoleName] FROM [RoleList]";
		#endregion

		#region IRoleListProvider Members
		public Core.Business.RoleList Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectRoleList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.RoleList roleList = new Core.Business.RoleList();

				if (!reader.IsDBNull(0)) roleList.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) roleList.RoleName = reader.GetString(1);

				return roleList;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.RoleList roleList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RoleName", SqlDbType.NVarChar, roleList.RoleName);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertRoleList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) roleList.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.RoleList roleList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@RoleName", SqlDbType.NVarChar, roleList.RoleName);
			sql.AddParameter("@Id", SqlDbType.Int, roleList.Id);
			sql.ExecuteSql(SqlUpdateRoleList);
		}

		public void Delete(Core.Business.RoleList roleList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, roleList.Id);
			sql.ExecuteSql(SqlDeleteRoleList);
		}

		public IList<Core.Business.RoleList> GetAllRoleList()
		{
			IList<Core.Business.RoleList> roleListlist = new List<Core.Business.RoleList>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllRoleList);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.RoleList roleList = new Core.Business.RoleList();

					if (!reader.IsDBNull(0)) roleList.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) roleList.RoleName = reader.GetString(1);

					roleList.MarkOld();
					roleListlist.Add(roleList);
				}
				reader.Close();
			}
			return roleListlist;
		}
		#endregion
	}
}
